package wsp.dailymarket.obj;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.DataSource;

/**
 * User profile bean class
 * @author Matt
 */
public class UserProfileBean {

	public static final int CLIENT_TYPE_CUSTOMER = 0xFA01;
	public static final int CLIENT_TYPE_PARTNER = 0xFA02;
	public static final int CLIENT_TYPE_ADMIN = 0xFA03;
	private String mEmailAddress;
	private String mFirstName;
	private String mLastName;
	private String mCompanyName;
	private String mAddressLine1;
	private String mAddressLine2;
	private String mAddressCity;
	private String mAddressState;
	private String mAddressZipCode;
	private int mClientType;

	/**
	 * Populated Constructor
	 * @param emailAddress
	 * @param firstName
	 * @param lastName
	 * @param companyName
	 * @param addressLine1
	 * @param addressLine2
	 * @param addressCity
	 * @param addressState
	 * @param addressZipCode
	 * @param clientType
	 */
	public UserProfileBean(
			String emailAddress,
			String firstName,
			String lastName,
			String companyName,
			String addressLine1,
			String addressLine2,
			String addressCity,
			String addressState,
			String addressZipCode,
			int clientType) {
		mEmailAddress = emailAddress;
		mFirstName = firstName;
		mLastName = lastName;
		mCompanyName = companyName;
		mAddressLine1 = addressLine1;
		mAddressLine2 = addressLine2;
		mAddressCity = addressCity;
		mAddressState = addressState;
		mAddressZipCode = addressZipCode;
		mClientType = clientType;
	}

	/**
	 * Empty constructor
	 */
	public UserProfileBean() {
		this("", "", "", "", "", "", "", "", "", 0);
	}

	public String getEmailAddress() {
		return this.mEmailAddress;
	}

	public String getFirstName() {
		return this.mFirstName;
	}

	public void setFirstName(String newValue) {
		this.mFirstName = newValue;
	}

	public String getLastName() {
		return this.mLastName;
	}

	public void setLastName(String newValue) {
		this.mLastName = newValue;
	}

	public String getCompanyName() {
		return this.mCompanyName;
	}

	public void setCompanyName(String newValue) {
		this.mCompanyName = newValue;
	}

	public String getAddressLine1() {
		return mAddressLine1;
	}

	public void setAddressLine1(String newValue) {
		this.mAddressLine1 = newValue;
	}

	public String getAddressLine2() {
		return mAddressLine2;
	}

	public void setAddressLine2(String newValue) {
		this.mAddressLine2 = newValue;
	}

	public String getAddressCity() {
		return mAddressCity;
	}

	public void setAddressCity(String newValue) {
		this.mAddressCity = newValue;
	}

	public String getAddressState() {
		return mAddressState;
	}

	public void setAddressState(String newValue) {
		this.mAddressState = newValue;
	}

	public String getAddressZipCode() {
		return mAddressZipCode;
	}

	public void setAddressZipCode(String newValue) {
		this.mAddressZipCode = newValue;
	}

	public int getClientType() {
		return mClientType;
	}

	public String getClientTypeDisplay() {
		return mClientType == CLIENT_TYPE_ADMIN ? "Administrator" : mClientType == CLIENT_TYPE_PARTNER ? "Partner" : "Customer";
	}

	public String getDisplayName() {
		// Determine display name
		String displayName = this.mEmailAddress;
		if (this.mClientType == UserProfileBean.CLIENT_TYPE_PARTNER) { // Partners
			displayName = this.mCompanyName;
		} else { // Customers
			if (this.mFirstName != null && this.mLastName != null) {
				displayName = this.mFirstName + " " + this.mLastName;
			} else if (this.mFirstName != null) {
				displayName = this.mFirstName;
			} else if (this.mLastName != null) {
				displayName = this.mLastName;
			}
		}
		return displayName;
	}

	public boolean isAdmin() {
		return this.mClientType == CLIENT_TYPE_ADMIN;
	}

	public boolean isPartner() {
		return this.mClientType == CLIENT_TYPE_PARTNER;
	}

	public void loadProfile(DataSource ds, String emailAddress, int clientType) throws SQLException, IllegalArgumentException {

		// Prepare bean
		this.mEmailAddress = emailAddress;
		this.mClientType = clientType;

		Connection con = null;
		try {
			con = ds.getConnection();
			Statement stmt = con.createStatement();
			String sql = "SELECT * FROM APP.CLIENTS WHERE client_id = '" + this.mEmailAddress + "' and client_type = " + this.mClientType;
			ResultSet rs = stmt.executeQuery(sql);
			if (rs.next()) {
				this.mFirstName = rs.getString("firstname"); // TODO Test null values, the string "null", etc
				this.mLastName = rs.getString("lastname");
				this.mCompanyName = rs.getString("companyname");
				this.mAddressLine1 = rs.getString("address_line1");
				this.mAddressLine2 = rs.getString("address_line2");
				this.mAddressCity = rs.getString("address_city");
				this.mAddressState = rs.getString("address_state");
				this.mAddressZipCode = rs.getString("address_zipcode");
			} else {
				throw new IllegalArgumentException("E-mail addres not found!");
			}
		} finally {
			if (con != null) {
				con.close();
			}
		}
	}

	public void saveProfile(DataSource ds) throws Exception {
		// Save to database
		Connection conn = null;
		try {
			conn = ds.getConnection();
			Statement stmt = conn.createStatement();
			String sql = "UPDATE APP.CLIENTS SET "
					+ "FIRSTNAME = '"
					+ this.mFirstName
					+ "', "
					+ "LASTNAME = '"
					+ this.mLastName
					+ "', "
					+ "COMPANYNAME = '"
					+ this.mCompanyName
					+ "', "
					+ "ADDRESS_LINE1 = '"
					+ this.mAddressLine1
					+ "', "
					+ "ADDRESS_LINE2 = '"
					+ this.mAddressLine2
					+ "', "
					+ "ADDRESS_CITY = '"
					+ this.mAddressCity
					+ "', "
					+ "ADDRESS_STATE = '"
					+ this.mAddressState
					+ "', "
					+ "ADDRESS_ZIPCODE = '"
					+ this.mAddressZipCode
					+ "' "
					+ "WHERE CLIENT_ID = '" + this.mEmailAddress + "'";
			if (stmt.executeUpdate(sql) != 1) {
				throw new Exception("Invalid user ID.");
			}
		} finally {
			if (conn != null) {
				conn.close();
			}
		}
	}
}
